<?php
/*
 *  兑换码
 */
class ZhanjianController extends CController {
	
	public function actionIndex()
	{
		$code=Tool::getrequestValue('code');
		$opid= WeixinApi::getaccess_token('wx0ef4de81b196d7f0','8dd46b65a0fb904e153c479cfcacceb6',$code);
		if($opid=='获取access_token错误'){
		
			echo  "getCode Error:access_token error";
			exit();
		}  
		
	    Yii::app()->cache->set('111', '22222');
	    
		$this->render('zhanjian_index',array('user_openid'=>'9999'));
	}
	
	
	public function actionGetCode(){
		 $openid=Tool::getrequestValue('openid');
		
	if(empty($openid)){
		echo  '{"code":3,"msg":"领取错误!"}';
		Yii::app()->end();
	}
	
	$check=Zhanjiancode::model()->find("openid =? ",array($openid));
	if(!empty($check)){
		echo  '{"code":4,"msg":"您已领取过了.","code_sn":"'.$check->code.'"}';
		Yii::app()->end();
	}
	
		$ex=ZhanjianCode::model()->find("openid is null");
		if(empty($ex)){
			echo  '{"code":3,"msg":"兑换码已领完"}';
			Yii::app()->end();
			
		}
		
		$ex->openid=$openid;
		$ex->save();
		
		echo  '{"code":0,"msg":"'.$ex->code.'"}';
		
		
	}
	
	
	
	
	public function actionaddEx(){
		
$code='';		
		$arr= explode(",",$code);
		$temp=null;
		foreach ($arr as $code){
			if(!empty($code)){
				$temp=new Exchangecode();
				$temp->code=$code;
				$temp->save();
			}
			
		}
		
		
		
		
	}
	public function actiontest(){
		
		$this->render('zhanjian_index_bak',array('user_openid'=>$opid));
		
	}
		
}
